package com.simtop.controller;

import com.simtop.common.ServerResponse;
import com.simtop.pojo.U3DEnglish;
import com.simtop.pojo.User;
import com.simtop.service.U3DEnglishService;
import com.simtop.util.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;


@Controller
@RequestMapping("/u3d/english")
@CrossOrigin
public class U3DEnglishController {
    private static final Logger logger = LoggerFactory.getLogger(U3DEnglishController.class);

    @Autowired
    private U3DEnglishService u3DEnglishService;

    /**
     * U3D<<<Web
     * @param u3DEnglish
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> addEnglishData(@RequestBody U3DEnglish u3DEnglish, HttpServletRequest request){
        logger.info("u3d正在往web数据库插入报告数据"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        System.out.println("获取到的实验记录参数为："+u3DEnglish);
        String token = request.getHeader("Authorization");
        System.out.println(token);
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt, User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return u3DEnglishService.add(u3DEnglish);
    }

    @RequestMapping(value = "/findByExpId",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<U3DEnglish> findByExpId(Integer experimentId, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt, User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return u3DEnglishService.findByExpId(experimentId);
    }
}
